'弹出文件夹选择窗口
' @optional-param default folder path
' @return selected folder
Public Function dirSelectDialog(Optional ByRef defaultDir As String) As String
    With Application.FileDialog(msoFileDialogFolderPicker)

        If defaultDir <> "" And UtilsIO.pathExist(defaultDir) = True Then
            If UtilsString.endsWith(defaultDir, "\") = False Then
                .InitialFileName = defaultDir & "\"
            Else
                .InitialFileName = defaultDir
            End If

        End If

        .Title = "Please Select Folder"
        .Show

        If .SelectedItems.count = 1 Then
            dirSelectDialog = .SelectedItems(1)
        Else
            dirSelectDialog = defaultDir
        End If
    End With
End Function

' 弹出文件保存窗口
' @param defaultPath 默认路径
' @param types 文件类型
' @return 保存文件的路径
Public Function fileSaveDialog(ByRef defaultPath As String, ParamArray types()) As String
    Dim fileFilterStr as String

    Dim argsCount As Integer
    Dim temp As String
    argsCount = UBound(types)
    If argsCount = -1 Then
        '[]
        fileFilterStr = ""
    Else
        '["aaa", "bbb"]

        Dim i As Integer
        For i = 0 To argsCount
            temp = types(i)

            fileFilterStr = fileFilterStr & temp & " Files (*." & temp & "), *." & temp & ","
        Next
    End If

    If defaultPath <> "" And UtilsIO.pathExist(defaultPath) = True Then
        if fileFilterStr = "" Then
            fileSaveDialog = Application.GetSaveAsFilename(initialFilename:=defaultPath, _
                                                    Title:="Save as")
        Else
            fileSaveDialog = Application.GetSaveAsFilename(initialFilename:=defaultPath, _
                                                    FileFilter:=fileFilterStr, _
                                                    Title:="Save as")
        End If
    Else
        if fileFilterStr = "" Then
            fileSaveDialog = Application.GetSaveAsFilename(Title:="Save as")
        Else
            fileSaveDialog = Application.GetSaveAsFilename(FileFilter:=fileFilterStr, _
                                                    Title:="Save as")
        End If
    End If

    If fileSaveDialog = "False" Then
        fileSaveDialog = ""
    End If

End Function

' 弹出文件选择窗体
' @param moreDiaFlg 是否允许选择多个文件
' @param-option defaultPath 默认显示的文件路径
' @param-option defaultType 默认显示的文件类型（只需要提供后缀即可。如 a.txt, 需要提供 "txt"）
' @return 选择的文件路径
Public Function fileSelectDialog(Optional ByRef moreDiaFlg As Boolean = False, Optional ByRef defaultPath As String, Optional ByRef defaultType As String) As String
    With Application.FileDialog(msoFileDialogFilePicker)

        .AllowMultiSelect = moreDiaFlg

        If defaultPath <> "" And UtilsIO.pathExist(defaultPath) = True Then
            .InitialFileName = defaultPath
        End If

        .Filters.Clear

        If defaultType <> "" Then
            .Filters.Add "defaultType", "*." & defaultType
        End If

        .Filters.Add "All Files", "*.*"

        .Title = "Please Select File"
        .Show

        If .SelectedItems.count = 1 Then
            fileSelectDialog = .SelectedItems(1)
        Else
            fileSelectDialog = defaultPath
        End If
    End With
End Function
